<template>
  <div class="user">
    <!--搜索表单-->
    <div class="common-seach-wrap">
      <el-form
        size="small"
        :inline="true"
        :model="formInline"
        class="demo-form-inline"
      >
        <el-form-item label="关键词">
          <el-input
            v-model="formInline.search"
            placeholder="请输入昵称|手机号|姓名"
          ></el-input>
        </el-form-item>
        <el-form-item label="奖品信息">
          <el-input
            v-model="formInline.recordName"
            placeholder="请输入奖品名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-select
            size="small"
            v-model="formInline.status"
            placeholder="请选择"
          >
            <el-option label="全部" :value="-1"></el-option>
            <el-option label="已兑换" :value="1"></el-option>
            <el-option label="未兑换" :value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="时间">
          <div class="block">
            <span class="demonstration"></span>
            <el-date-picker
              v-model="formInline.reg_date"
              type="daterange"
              value-format="YYYY-MM-DD"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </div>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="onSubmit"
            >查询</el-button
          >
          <el-button size="small" type="success" @click="onExport"
            >导出</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <!--内容-->
    <div class="product-content">
      <div class="table-wrap">
        <el-table
          size="small"
          :data="tableData"
          border
          style="width: 100%"
          v-loading="loading"
        >
          <el-table-column
            prop="userId"
            label="ID"
            width="80"
          ></el-table-column>
          <el-table-column prop="nickName" label="昵称"></el-table-column>
          <el-table-column label="微信头像">
            <template #default="scope">
              <template v-if="scope.row.avatarUrl">
                <img :src="scope.row.avatarUrl" :width="30" :height="30" />
              </template>
            </template>
          </el-table-column>
          <el-table-column prop="mobile" label="手机号"></el-table-column>
          <el-table-column prop="recordName" label="中奖内容"></el-table-column>
          <el-table-column
            prop="lotteryTypeText"
            label="中奖类型"
          ></el-table-column>
          <el-table-column prop="status" label="状态">
            <template #default="scope">
              <span v-if="scope.row.status == 1">已兑换</span>
              <span class="red" v-if="scope.row.status == 0">未兑换</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="createTime"
            label="抽奖时间"
            width="140"
          ></el-table-column>
          <el-table-column prop="remark" label="备注"></el-table-column>
          <el-table-column fixed="right" label="操作" width="80">
            <template #default="scope">
              <el-button
                @click="sendClick(scope.row)"
                type="text"
                size="small"
                v-if="scope.row.status === 0 && scope.row.phone"
                >发放</el-button
              >
              <el-button
                v-if="scope.row.expressId > 0"
                @click="getLogistics(scope.row)"
                link
                type="primary"
                size="small"
                v-auth="'/order/order/express'"
                >查看物流
              </el-button>
              <el-button
                @click="remarks(scope.row)"
                link
                type="primary"
                size="small"
                >备注
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!--分页-->
      <div class="pagination">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          background
          :current-page="curPage"
          :page-size="pageSize"
          layout="total, prev, pager, next, jumper"
          :total="totalDataNumber"
        ></el-pagination>
      </div>
    </div>
    <!--处理-->
    <Send
      v-if="open_send"
      :open_send="open_send"
      :model="currentModel"
      :expressList="expressList"
      @closeDialog="closeDialogFunc($event, 'Send')"
    ></Send>
    <Logistics
      v-if="isLogistics"
      :logisticsData="logisticsData"
      :isLogistics="isLogistics"
      @closeDialog="closeLogistics"
    ></Logistics>
    <remarks
      v-if="is_remark"
      :is_remark="is_remark"
      :Val="remarkVal"
      @closeDialog="closeDialogFunc($event, 'remark')"
    ></remarks>
  </div>
</template>

<script>
import LotteryApi from "@/api/lottery.js";
import Send from "./dialog/Send.vue";
import remarks from "./dialog/remarks.vue";
import qs from "qs";
import { useUserStore } from "@/store";
const { token } = useUserStore();
import Logistics from "@/components/logistics/viewLogistics.vue";
export default {
  components: {
    Send,
    Logistics,
    remarks,
  },
  data() {
    return {
      token,
      /*是否加载完成*/
      loading: true,
      /*列表数据*/
      tableData: [],
      /*一页多少条*/
      pageSize: 20,
      /*一共多少条数据*/
      totalDataNumber: 0,
      /*当前是第几页*/
      curPage: 1,
      /*横向表单数据模型*/
      formInline: {
        search: "",
        reg_date: "",
        status: "",
        recordName: "",
      },
      open_send: false,
      currentModel: {},
      expressList: [],
      logisticsData: {},
      isLogistics: false,
      is_remark: false,
      remarkVal: {},
    };
  },
  created() {
    /*获取列表*/
    this.getTableList();
  },
  methods: {
    remarks(e) {
      this.remarkVal = e;
      this.is_remark = true;
    },
    openLogistics() {
      this.isLogistics = true;
      console.log(this.isLogistics);
    },
    closeLogistics() {
      this.isLogistics = false;
    },
    getLogistics(row) {
      let self = this;
      let Params = {
        recordId: row.recordId,
        expressId: row.expressId,
        expressNo: row.expressNo,
      };
      self.loading = true;
      LotteryApi.express(Params, true)
        .then((res) => {
          self.loading = false;
          self.logisticsData = res.data.express.data;
          self.openLogistics();
        })
        .catch((error) => {
          self.loading = false;
        });
    },
    onExport: function () {
      let self = this;
      let Params = {
        search: self.formInline.search,
        recordName: self.formInline.recordName,
        status: self.formInline.status,
        pageIndex: self.curPage,
        pageSize: self.pageSize,
      };
      Params.tokenshop = self.token;
      let baseUrl = import.meta.env.VITE_BASIC_URL;
      if (!baseUrl) {
        baseUrl = window.location.protocol + "//" + window.location.host;
      }
      window.location.href =
        baseUrl + "/api/shop/plus/lottery/recordExport?" + qs.stringify(Params);
    },
    /*选择第几页*/
    handleCurrentChange(val) {
      let self = this;
      self.curPage = val;
      self.loading = true;
      self.getTableList();
    },

    /*每页多少条*/
    handleSizeChange(val) {
      this.curPage = 1;
      this.pageSize = val;
      this.getTableList();
    },
    /*获取列表*/
    getTableList() {
      let self = this;
      let Params = self.formInline;
      Params.pageIndex = self.curPage;
      Params.pageSize = self.pageSize;
      if (self.formInline.reg_date != "") {
        Params.startTime = Params.reg_date[0];
        Params.endTime = Params.reg_date[1];
      }
      LotteryApi.recordList(Params, true)
        .then((res) => {
          self.loading = false;
          self.tableData = res.data.list.records;
          self.totalDataNumber = res.data.list.total;
          self.expressList = res.data.expressList;
        })
        .catch((error) => {
          self.loading = false;
        });
    },
    /*搜索查询*/
    onSubmit() {
      let self = this;
      self.loading = true;
      self.curPage = 1;
      self.getTableList();
    },
    sendClick(e) {
      this.open_send = true;
      this.currentModel = e;
    },
    /*关闭弹窗*/
    closeDialogFunc(e, f) {
      if (f == "Send") {
        this.open_send = e.openDialog;
        if (e.type == "success") {
          this.getTableList();
        }
      } else if (f == "remark") {
        this.is_remark = e.openDialog;
        if (e.type == "success") {
          this.getTableList();
        }
      }
    },
  },
};
</script>
<style></style>
